<?php
header('Access-Control-Allow-Origin:*'); //注意！跨域要加这个头 
require_once "./conn.php";
// print_r($_REQUEST);
// $jsonArray = $_REQUEST['Json'];

// print_r($jsonArray);
// //--解析Json，获取对应的变量值
// // print_r(json_decode($jsonArray, TRUE));
// $jsonArray = json_decode($jsonArray, TRUE);
// print_r($jsonArray);
$jsonArray = file_get_contents('php://input');

// 解析JSON数据
$jsonArray = json_decode($jsonArray, TRUE);

if (json_last_error() !== JSON_ERROR_NONE) {
    die("Invalid JSON format");
}

print_r($jsonArray);
/* 
user_device_name=111&user_account=222&user_password=333&
user_zone_name=444&user_name=555&user_role=666&user_level=777&user_id=888&
user_sect=999&user_gold_coin=888&user_silver_coin=777&user_activity=666
&user_status=555&user_run_time=444&user_last_time=333 */
$id = $jsonArray['id'];
$user_device_name = $jsonArray['user_device_name'];
$user_account = $jsonArray['user_account'];
$user_password = $jsonArray['user_password'];
$user_zone_name = $jsonArray['user_zone_name'];
$user_name = $jsonArray['user_name'];
$user_role = $jsonArray['user_role'];
$user_level = $jsonArray['user_level'];
$user_id = $jsonArray['user_id'];

$user_sect = $jsonArray['user_sect'];
$user_gold_coin = $jsonArray['user_gold_coin'];
$user_silver_coin = $jsonArray['user_silver_coin'];
$user_activity = $jsonArray['user_activity'];
$user_status = $jsonArray['user_status'];
$user_run_time = $jsonArray['user_run_time'];
$user_last_time = $jsonArray['user_last_time'];

$comment = "";
$user_gem_hammer = isset($jsonArray['user_gem_hammer']) ? intval($jsonArray['user_gem_hammer']) : 0;// 宝石锤
$frozen_gold_coin = isset($jsonArray['frozen_gold_coin']) ? floatval($jsonArray['frozen_gold_coin']) : 0.00;// 冻结金币
// 判断有没有这个账号
$user_zhongkong_name = $jsonArray['user_zhongkong_name'];
// echo '<br>';
// echo $jsonArray['user_zhongkong_name'];
// echo "user_zhongkong_name";
// echo $user_zhongkong_name;
// echo '<br>';
$sql0 = $conn->query("select uname from users where uname ='{$user_zhongkong_name}'"); //查询
$row = mysqli_fetch_assoc($sql0);
if ($row > 0) {
    echo $row;


    // 先判断数据库有没有这个数据
    $sql1 = "SELECT id from roles where user_account = '" . $user_account . "' and user_device_name = '" . $user_device_name . "' and user_password = '" . $user_password . "' and user_zone_name = '" . $user_zone_name . "' and user_zhongkong_name = '" . $user_zhongkong_name . "'";
    // echo $sql1;
    $stmt = $conn->prepare($sql1);
    if ($stmt->execute()) {
        if ($rs = $stmt->get_result()) {

            if ($row = $rs->fetch_assoc()) {
                echo '有';
                // print_r($row);
                $id = $row['id'];
                $comment = $row['comment'];
            } else {
                echo "没有";
            }
        }
    }

    $sql = "REPLACE INTO roles(id, user_zhongkong_name, user_device_name, user_account, user_password, user_zone_name, user_name, user_role, user_level, user_id, user_sect, user_gold_coin, user_silver_coin, user_activity, user_status, user_run_time, user_last_time, update_time, comment, user_gem_hammer, frozen_gold_coin, gold_coin_growth) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), ?, ?, ?, ?)";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("issssssssssssssssssss", $id, $user_zhongkong_name, $user_device_name, $user_account, $user_password, $user_zone_name, $user_name, $user_role, $user_level, $user_id, $user_sect, $user_gold_coin, $user_silver_coin, $user_activity, $user_status, $user_run_time, $user_last_time, $comment, $user_gem_hammer, $frozen_gold_coin, $gold_coin_growth);
    if ($stmt->execute()) {
        echo "提交成功";
    } else {
        echo "提交失败,失败原因：" . $stmt->error;
    }
} else {
    echo '此中控账号不存在,请前往42.194.136.15注册';
}
/* 
[
{"user_device_name":"33","user_account":"3","user_password":"24","user_zone_name":"54","user_name":"4646","user_role":"464","user_level":"464","user_id":"4","user_sect":"4646","user_gold_coin":"4","user_silver_coin":"3","user_activity":"13","user_status":"13","user_run_time":"14","user_last_time":"534"}
]
*/
